package com.alibaba.citrus.ots.sales.plan.facade.service.setting;

import com.alibaba.citrus.ots.common.annotation.FacadeInvoker;
import com.alibaba.citrus.ots.sales.plan.api.setting.SalesPlanStrategyDetailQueryService;
import com.alibaba.citrus.ots.sales.plan.dto.api.request.PageQuerySalesPlanStrategyDetailRequest;
import com.alibaba.citrus.ots.sales.plan.dto.api.request.QuerySalesPlanStrategyDetailRequest;
import com.alibaba.citrus.ots.sales.plan.dto.api.request.QuerySalesPlanStrategyListRequest;
import com.alibaba.citrus.ots.sales.plan.dto.api.request.QuerySalesPlanStrategyRequest;
import com.alibaba.citrus.ots.sales.plan.dto.api.request.SalesOrgRatioDTO;
import com.alibaba.citrus.ots.sales.plan.dto.api.response.SalesPlanStrategyDetailResponse;
import com.alibaba.citrus.ots.sales.plan.repository.SalesPlanStrategyDetailRepository;
import com.alibaba.citrus.ots.sales.plan.repository.SalesPlanStrategyRepository;
import com.alibaba.citrus.ots.sales.plan.sdo.SalesPlanStrategyDetailSDO;
import com.alibaba.citrus.ots.sales.plan.sdo.SalesPlanStrategySDO;
import com.alibaba.cz.base.tool.page.PageResult;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SalesPlanStrategyDetailQueryServiceImpl implements SalesPlanStrategyDetailQueryService {

    @Resource
    private SalesPlanStrategyDetailRepository salesPlanStrategyDetailRepository;

    @Resource
    private SalesPlanStrategyRepository salesPlanStrategyRepository;

    @FacadeInvoker
    @Override
    public Result<PageResult<List<SalesPlanStrategyDetailSDO>>> pageQuerySalesPlanStrategyDetailList(PageQuerySalesPlanStrategyDetailRequest pageQuerySalesPlanStrategyDetailRequest) {
        PageResult<List<SalesPlanStrategyDetailSDO>> listPageResult =  salesPlanStrategyDetailRepository.pageQuery(pageQuerySalesPlanStrategyDetailRequest);
        return Result.success(listPageResult);
    }

    @FacadeInvoker
    @Override
    public Result<SalesPlanStrategyDetailSDO> queryProductPlanInfoById(QuerySalesPlanStrategyDetailRequest querySalesPlanStrategyDetailRequest) {
        SalesPlanStrategyDetailSDO salesPlanStrategyDetailSDO= salesPlanStrategyDetailRepository.querySalesPlanStrategyDetail(querySalesPlanStrategyDetailRequest);
        return Result.success(salesPlanStrategyDetailSDO);
    }

    @FacadeInvoker
    @Override
    public Result<SalesPlanStrategyDetailResponse> getSalesPlanStrategyDetail(QuerySalesPlanStrategyListRequest querySalesPlanStrategyListRequest) {
        // 获取策略id
        QuerySalesPlanStrategyRequest querySalesPlanStrategyRequest = new QuerySalesPlanStrategyRequest();
        querySalesPlanStrategyRequest.setOrgBusinessCode(querySalesPlanStrategyListRequest.getOrgBusinessCode());
        querySalesPlanStrategyRequest.setType(querySalesPlanStrategyListRequest.getType());
        SalesPlanStrategySDO salesPlanStrategySDO = salesPlanStrategyRepository.querySalesPlanStrategyByOrgBusinessCode(querySalesPlanStrategyRequest);
        if (null == salesPlanStrategySDO) {
            return null;
        }

        // 查询销售组织详情
        String features = salesPlanStrategySDO.getFeatures();
        if (StringUtils.isBlank(features)) {
            return null;
        }
        SalesPlanStrategyDetailResponse response = new SalesPlanStrategyDetailResponse();
        response.setOrgBusinessCode(salesPlanStrategySDO.getOrgBusinessCode());
        response.setType(salesPlanStrategySDO.getType());
        List<SalesOrgRatioDTO> salesOrgRatioDTOList = JSONArray.parseArray(features, SalesOrgRatioDTO.class);
        response.setSalesOrgRatioDTOList(salesOrgRatioDTOList);

        // 根据策略id查询计划回复策略详情
        PageQuerySalesPlanStrategyDetailRequest pageQueryRequest = new PageQuerySalesPlanStrategyDetailRequest();
        pageQueryRequest.setReplyStrategyId(salesPlanStrategySDO.getId());
        pageQueryRequest.setStart(0);
        pageQueryRequest.setLimit(500);
        pageQueryRequest.setStatus(1);
        Result<PageResult<List<SalesPlanStrategyDetailSDO>>> pageResult = pageQuerySalesPlanStrategyDetailList(pageQueryRequest);
        if (null == pageResult || null == pageResult.getResult() || pageResult.getResult().getTotal() < 1) {
            response.setSalesPlanStrategyDetailCustomerList(Lists.newArrayList());
            response.setSalesPlanStrategyDetailDepartmentList(Lists.newArrayList());
            Result.success(response);
        }
        List<SalesPlanStrategyDetailSDO> salesPlanStrategyDetailSDOList = pageResult.getResult().getContent();

        List<SalesPlanStrategyDetailSDO> customerList = salesPlanStrategyDetailSDOList.stream().filter(salesPlanStrategyDetailSDO -> salesPlanStrategyDetailSDO.getReplyStrategyType() == 2).collect(Collectors.toList());
        List<SalesPlanStrategyDetailSDO> departmentList = salesPlanStrategyDetailSDOList.stream().filter(salesPlanStrategyDetailSDO -> salesPlanStrategyDetailSDO.getReplyStrategyType() == 1).collect(Collectors.toList());
        response.setSalesPlanStrategyDetailCustomerList(customerList);
        response.setSalesPlanStrategyDetailDepartmentList(departmentList);
        return Result.success(response);
    }
}
